/*
 * WatchView.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Lucian Ghita Felix(felix@ulise.cs.pub.ro)
 *
 * Obs: N/A
 */

package ide;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

import middle.*;

@SuppressWarnings("serial")
public class WatchView extends JPanel { 
  
  public JTree watchTree;
  Project project;
    
  public WatchView(Project project) {
    
    
    super(); 
    JScrollPane scroller = new JScrollPane();
    vide.spl.showStatus("Creating watch browser..");
    this.project = project;
    project.wv = this;

    watchTree = new JTree(project.watchTreeRoot);
        //ar fi frumos daca tooltipuirile ar fi FQN-uri deci:
    ToolTipManager.sharedInstance().registerComponent(watchTree); 
        //This is not done automaticly!

    ((WatchTreeNode)project.watchTreeRoot.getUserObject()).tree = watchTree;

    watchTree.setShowsRootHandles(true);
    watchTree.setCellRenderer(new WatchTreeCellRenderer());
    scroller.getViewport().add(watchTree);
    setBackground(watchTree.getBackground());

    MouseListener ml = new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        int selRow = watchTree.getRowForLocation(e.getX(), e.getY());
        if((selRow != -1) && (e.getClickCount() == 1)) {
             addWaveForm();
          }
        }
      };
    watchTree.addMouseListener(ml);
    JPanel panel2 = new JPanel();
    panel2.setLayout(new BorderLayout());

    setLayout(new BorderLayout());
    add(scroller, BorderLayout.CENTER);
    add(panel2, BorderLayout.SOUTH);
  }

  public void clear() {
    DefaultTreeModel model = (DefaultTreeModel) watchTree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    for(Enumeration e = root.children() ; e.hasMoreElements() ;){
      DefaultMutableTreeNode tn = (DefaultMutableTreeNode)e.nextElement();
      model.removeNodeFromParent(tn); 
    }
    model.nodeStructureChanged(root);
  }
  
  public void addWaveForm() {
    TreePath selectedTreePath = watchTree.getSelectionPath();
    if (selectedTreePath == null) return;

    Object obj = selectedTreePath.getLastPathComponent();
    WatchTreeNode node = (WatchTreeNode) ((DefaultMutableTreeNode) obj).getUserObject();
    if (!node.isLeaf) return;
      //poate ca ar trebui sa poata fi adaugat in waveView un [scope] intreg (cu totzi membrii)
      //deocamdate nu pot fi adaugate decat frunzele.  //todo.

    addWaveForm(node.getFQN());
  }

  public void addWaveForm(String fqn){
    if(project.vcdReader == null) {
      System.out.println("Strange: VCDReader == null...Can't get variableHistory.");
      return;
    }
    VariableHistory vh = project.vcdReader.getVarHistory(fqn);
    if(vh != null) //this may happen if the saved variable list is no more up to date
      project.waveViewFrame.waveView.addWave(vh, fqn);
  }
  
  public class AddWaveForm extends AbstractAction {
    public AddWaveForm() {
      super("Add Wave Form");setEnabled(true);
      putValue(SMALL_ICON, new ImageIcon(vide.baseDir + "images/newWave.gif"));  
      putValue(LONG_DESCRIPTION, "Adds a wave form viewer for the selected variable.");
    }
    public void actionPerformed(java.awt.event.ActionEvent e) {
      addWaveForm();
    };
  }
}


